package com.example.uit_management;

import java.util.Calendar;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	/**Bien luu tên dang nhap*/
	static String Student_ID="08520211";
	/**Bien luu mật khẩu*/
	static String Student_ID_pass="00000";
	/**Bien button dang nhap*/
	private Button bt;
	/**Bien button thoat*/
	private Button bt_exit;
	/**Bien Edittext Username*/
	private EditText EText_user;
	/**Bien Edittext Password*/
	private EditText EText_pass;
	/** Bien TextView Changepass */
	private TextView TextView_changepass;
	/** Bien thu trong tuan */
	static public int day;	
	/**Biến kiểu  JsonParser_StudentPass*/
	JsonParser_StudentPass jsonParser_StudentPass= new JsonParser_StudentPass();
	private static final String SPF_NAME = "vidslogin";
	public static final String USER = "username";
	public static final String PWD = "password";
	private CheckBox checkpass;
	@SuppressLint("NewApi")
	@Override
    public void onCreate(Bundle savedInstanceState) {
    	StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    	StrictMode.setThreadPolicy(policy);
    	Log.e(MainActivity.class.toString(), "qweqwe");	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);        
        // Get ID of button Login
        bt=(Button) findViewById(R.id.button_login);  
        // Get ID of button exit
        bt_exit=(Button) findViewById(R.id.button_exit);
        // Get ID of EditText User
        EText_user=(EditText) findViewById(R.id.editText_name);
        // Get ID of EditText Pass
        EText_pass=(EditText) findViewById(R.id.editText_password);
        // Get ID of TextView changepass
        TextView_changepass =(TextView) findViewById(R.id.textView_changepass);
        // Set listener for dang nhap button
        bt.setOnClickListener(btnNewListener);        
        // Set listener for thoat button
        checkpass=(CheckBox) findViewById(R.id.checkBox_remember_passs);
        SharedPreferences loginPreferences = getSharedPreferences(SPF_NAME,
                Context.MODE_PRIVATE);
        EText_user.setText(loginPreferences.getString(USER, ""));
        EText_pass.setText(loginPreferences.getString(PWD, ""));
        Log.e(MainActivity.class.toString(),"thu"+ loginPreferences.getString(USER, ""));	
        Log.e(MainActivity.class.toString(),"thu"+ loginPreferences.getString(PWD, ""));
        if(EText_user.getText().toString().equals("")){
        	checkpass.setChecked(false);
        }else{
        	checkpass.setChecked(true);
        }
        bt_exit.setOnClickListener(bt_exit_NewListener); 
        TextView_changepass.setOnClickListener(TextView_Changepass_NewListener); 
        if(checkInternetConnection())
    	{
    		Toast.makeText(this, "Kết nối Internet thành công", Toast.LENGTH_LONG).show();
    	}else 
    		Toast.makeText(this, "Kết nối Internet thất bại", Toast.LENGTH_LONG).show();
        Calendar calendar = Calendar.getInstance();
        day = calendar.get(Calendar.DAY_OF_WEEK); 
		Log.e(MainActivity.class.toString(),"thu"+ day);	
		

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    /**
    Method btnNewListener 
    Listener for Login button
    Show list Menu for student choose
    */
    private OnClickListener btnNewListener = new OnClickListener()
    {    	
        public void onClick(View v)        {       
        	
        	Log.e(MainActivity.class.toString(),"4.1");
        	//String result = jsonParser_StudentPass.Check_StudentPass(EText_user.getText().toString(),EText_pass.getText().toString());
        	String result ="false1";
        	result = jsonParser_StudentPass.Check_StudentPass(EText_user.getText().toString(),EText_pass.getText().toString());       	        	      	
        	Log.e(MainActivity.class.toString(),"Kết quả kiểm tra mật khẩu: "+result);
        	Log.e(MainActivity.class.toString(),"4.3");
        	if(checkpass.isChecked()==true)	{
        		SharedPreferences loginPreferences = getSharedPreferences(SPF_NAME, Context.MODE_PRIVATE);
                loginPreferences.edit().putString(USER, EText_user.getText().toString()).putString(PWD, EText_pass.getText().toString()).commit();
            	
        	}else
        	{
        		SharedPreferences loginPreferences = getSharedPreferences(SPF_NAME, Context.MODE_PRIVATE);
        		loginPreferences.edit().clear().commit();
        	}        	
        	if(result.equals("true")){
	        		
        		//Lay ma so sinh vien
        		Log.e(MainActivity.class.toString(),"4.3");
        		Student_ID=EText_user.getText().toString();
            //Lay mat khau
        		Student_ID_pass=EText_pass.getText().toString();
        		Log.e(MainActivity.class.toString(),"4.4");
        		finish();  
        		Log.e(MainActivity.class.toString(),"4.5");
        		Intent intent = new Intent(getApplicationContext(),Menu_GridView.class);
        		startActivity(intent);	
        	}else{
        		 //Tao mot dialog thong bao la password sai
        		Log.e(MainActivity.class.toString(),"4.6");
        		 AlertDialog.Builder AlertDialog = new AlertDialog.Builder(MainActivity.this);
  	          	 AlertDialog.setTitle("Thông báo");
  	          	 AlertDialog.setMessage("Mật khẩu không đúng");
  	          	 // lắng nghe cho nút ok trong dialog
  	          	 AlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						
					}
				});
 	          	Log.e(MainActivity.class.toString(),"4.7");
 	          	AlertDialog.show(); // lenh show dialog
//  	          	Log.e(MainActivity.class.toString(),"4.9");
//  	          	finish();  
//  	            Log.e(MainActivity.class.toString(),"4.10");
//  	          	Intent intent = new Intent(getApplicationContext(),Menu_GridView.class);
//  				startActivity(intent);
//  				Log.e(MainActivity.class.toString(),"4.11");
        	}        	
        }
    }; 
    /**
    Method TextView_Changepass_NewListener
    Thay doi mat khau
    */
    private OnClickListener TextView_Changepass_NewListener = new OnClickListener()
    {
        public void onClick(View v)
        {         	finish(); 
        	   	Intent intent = new Intent(getApplicationContext(),Activity_ChangePass.class);
  				startActivity(intent);        	
        }
    }; 
    /**
    Method btnNewListener 
    Listener for Login button
    Show list Menu for student choose
    */
    private OnClickListener bt_exit_NewListener = new OnClickListener()
    {
        public void onClick(View v)
        {         	
        	finish();
            System.exit(0);
        } 	
    }; 
 // Ham kiem tra co ket noi Internet khong
 	private boolean checkInternetConnection() { 
 	ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE);    
 	if (conMgr.getActiveNetworkInfo() != null         && conMgr.getActiveNetworkInfo().isAvailable()  
 	&& conMgr.getActiveNetworkInfo().isConnected()) {    
 	return true;         
 	} 
 	else {      
 	return false;       
 	}   
 	}
 	
}

